package com.cbwl.eoas.cms.service.impl.sysconfig;



import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.cbwl.eoas.cms.api.sysconfig.ISysUserService;
import com.cbwl.eoas.cms.dao.sysconfig.ISysEmployeeDao;
import com.cbwl.eoas.cms.dao.sysconfig.ISysResourceDao;
import com.cbwl.eoas.cms.dao.sysconfig.ISysUserDao;
import com.cbwl.eoas.cms.dao.sysconfig.ISysUserLogDao;
import com.cbwl.eoas.cms.model.sysconfig.SysEmployeeDTO;
import com.cbwl.eoas.cms.model.sysconfig.SysResourceDTO;
import com.cbwl.eoas.cms.model.sysconfig.SysUserDTO;
import com.cbwl.eoas.cms.model.sysconfig.SysUserLogDTO;
import com.cbwl.eoas.common.base.BaseServiceImpl;
import com.cbwl.eoas.common.framework.session.SecurityUserDTO;
import com.cbwl.eoas.common.util.Md5Util;
import com.cbwl.eoas.common.util.constain.DicDetailConstant;


/**
 * 
 * <p>[描述信息：SysUserDAO接口]</p>
 *
 * @author 虞云波 - yuyunbo@3856.cc
 * @version 1.0 Created on 2017年10月12日 下午13:29:23
 */
@Service("sysUserService")
public class SysUserServiceImpl  extends BaseServiceImpl<SysUserDTO>  implements ISysUserService  {

	@Autowired
	private ISysUserDao isysUserDao;
	
	@Autowired
	private ISysResourceDao isysResourceDao;
	
	@Autowired
	private ISysUserLogDao isysUserLogDao;
	
	@Autowired
	private ISysEmployeeDao isysEmployeeDao;
	
	
	
	/**
	 * 
	 * <p>根据用户属性查询用户信息</p>
	 * 
	 * @param bean
	 * @return
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @date: Created on 2017年10月23日 上午11:40:20
	 */
	@Override
	public SysUserDTO getUserDTOByParam(SysUserDTO bean) {
		return isysUserDao.getUserDTOByParam(bean);	
    }


	/**
	 * 
	 * <p>根据工号查询出用户基础信息,保存到缓存中</p>
	 * 
	 * @see com.cbwl.eoas.cms.api.sysconfig.ISysUserService#getSecurityUserByLoginName(com.cbwl.eoas.cms.model.sysconfig.SysUserDTO)
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @date: Created on 2017年10月23日 下午6:01:07
	 */
	@Override
	public SecurityUserDTO getSecurityUserByLoginName(SysUserDTO bean) {
		return isysUserDao.getSecurityUserByLoginName(bean);
	}

	
	/**
	 * 
	 * <p>根据用户id查询用户资源信息</p>
	 * 
	 * @see com.cbwl.eoas.cms.api.sysconfig.ISysUserService#queryObjectListByUid(com.cbwl.eoas.cms.model.sysconfig.SysResourceDTO)
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @date: Created on 2017年11月2日 上午10:10:45
	 */
	@Override
	public List<SysResourceDTO> queryObjectListByUid(SysResourceDTO bean) {
		return isysResourceDao.queryObjectListByUid(bean);
	}

	
	/**
	 * 
	 * <p>更新用户信息</p>
	 * 
	 * @see com.cbwl.eoas.cms.api.sysconfig.ISysUserService#updateUserInfo(com.cbwl.eoas.cms.model.sysconfig.SysUserDTO)
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @throws Exception 
	 * @date: Created on 2017年11月2日 上午10:10:31
	 */
	@Override
	public long updateUserInfo(SysUserDTO bean) throws Exception {
		
		if(!StringUtils.isEmpty(bean.getUserPhone())){
			// EOAS TODO 用户修改手机号日志
			SysUserLogDTO beanLog = new SysUserLogDTO();
			beanLog.setOperationType(DicDetailConstant.operation_type_2);
			beanLog.setUserId(bean.getModifier());
			beanLog.setCreater(bean.getModifier());
			beanLog.setGmtCreate( new Date());
			beanLog.setNewinfo(bean.getUserPhone());
			isysUserLogDao.saveObject(beanLog);
		}
		
		if(!StringUtils.isEmpty(bean.getUserPassword())){
			// EOAS TODO 用户修改密码日志
			bean.setUserPassword(Md5Util.encodePassword(bean.getUserPassword(), bean.getUserJobno()));
			
			SysUserLogDTO beanLog = new SysUserLogDTO();
			beanLog.setOperationType(DicDetailConstant.operation_type_1);
			beanLog.setUserId(bean.getModifier());
			beanLog.setCreater(bean.getModifier());
			beanLog.setGmtCreate( new Date());
			beanLog.setNewinfo(bean.getUserPassword());
			isysUserLogDao.saveObject(beanLog);
		}
		
		//更新用户头像
		if(!StringUtils.isEmpty(bean.getEmpAvatar())){
			
			//查询出职员id
			SysUserDTO beanUser = isysUserDao.getObjectById(bean);
			
			//更具用户id，查询出职员id
			SysEmployeeDTO empBean = new SysEmployeeDTO();
			if(StringUtils.isEmpty(beanUser.getEmpId())){
				return 0;
			}
			empBean.setId(beanUser.getEmpId());
			empBean.setEmpAvatar(bean.getEmpAvatar());
			empBean.setModifier(beanUser.getId());
			empBean.setGmtModified(new Date());
			
			return  isysEmployeeDao.updateObject(empBean);
		}
		
		return isysUserDao.updateObject(bean);
	}


	/**
	 * 
	 * <p>密码重置</p>
	 * 
	 * @see com.cbwl.eoas.cms.api.sysconfig.ISysUserService#updateUserPassWord(com.cbwl.eoas.cms.model.sysconfig.SysUserDTO)
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @throws Exception 
	 * @date: Created on 2017年12月6日 下午3:25:05
	 */
	@Override
	public int updateUserPassWord(SysUserDTO bean) throws Exception {
		
		if(StringUtils.isEmpty(bean.getId())){
			return 0;
		}
		
		SysUserDTO uBean = isysUserDao.getObjectById(bean);
		
		bean.setUserPassword(Md5Util.encodePassword("666666", uBean.getUserJobno()));
		SysUserLogDTO beanLog = new SysUserLogDTO();
		beanLog.setOperationType(DicDetailConstant.operation_type_1);
		beanLog.setUserId(bean.getModifier());
		beanLog.setCreater(bean.getModifier());
		beanLog.setGmtCreate( new Date());
		beanLog.setNewinfo(bean.getUserPassword());
		beanLog.setRemark("密码重置");
		isysUserLogDao.saveObject(beanLog);
		
		return isysUserDao.updateObject(bean);
	}
	
	
}
